package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.AnswerService;
import com.jsb.service.QuestionService;
import com.jsb.service.SecretlyService;
import com.jsb.service.UserDealRecordService;
import com.jsb.service.UserService;
import com.jsb.dto.UserDealRecordComplex;
import com.jsb.dto.UserQuestion;
import com.jsb.dto.UserSecretly;
import com.jsb.model.Answer;
import com.jsb.model.AnswerExample;
import com.jsb.model.Question;
import com.jsb.model.Secretly;
import com.jsb.model.SecretlyExample;
import com.jsb.model.User;
import com.jsb.model.UserDealRecord;
import com.jsb.model.UserDealRecordExample;
import com.jsb.model.UserDealRecordExample.Criteria;

@Controller
public class SecretlyController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/secretly/list.do";
	
	@Resource
	private SecretlyService secretlyService;
	@Resource
	private UserService userService;
	@Resource
	private QuestionService questionService;
	@Resource
	private UserDealRecordService userDealRecordService;
	@Resource
	private AnswerService answerService;
	
	@RequestMapping("/secretly/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		SecretlyExample secretlyExample = new SecretlyExample();
		int totalCount = secretlyService.countSecretly(secretlyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		secretlyExample.setPageIndex((pageNum - 1) * pageSize);
		secretlyExample.setPageSize(pageSize);
		List<Secretly> secretlys = secretlyService.getPageSecretly(secretlyExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("secretlys", secretlys);
		return "/Secretly/list";
	}
	
	@RequestMapping("/secretly/toAdd")
	public String toAdd(Model model) {
		return "/Secretly/add";
	}

	@RequestMapping("/secretly/add")
	public String add(Model model, @ModelAttribute("secretly") Secretly secretly) {
		secretlyService.addSecretly(secretly);
		return redirect;
	}

	@RequestMapping("/secretly/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		secretlyService.delSecretlyByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/secretly/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Secretly secretly = secretlyService.getSecretlyByUuid(uuid);
		model.addAttribute("secretly", secretly);
		return "/Secretly/edit";
	}

	@RequestMapping("/secretly/edit")
	public String edit(Model model, @ModelAttribute("secretly") Secretly secretly) {
		secretlyService.updateSecretly(secretly);
		return redirect;
	}

	@RequestMapping("/secretly/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Secretly secretly = secretlyService.getSecretlyByUuid(uuid);
		model.addAttribute("secretly", secretly);
		return "/Secretly/view";
	}
	
	@RequestMapping("/secretly/viewDetail")
	public String viewDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		
		//偷偷看的对象
		Secretly secretly = secretlyService.getSecretlyByUuid(uuid);
		UserSecretly userSecretly= new UserSecretly();
		userSecretly.setSecretly(secretly);
		String peepPhoneNum = secretly.getPeepPhoneNum();
		//偷偷看的人
		User peepUser = userService.getUserByPhoneNumber(peepPhoneNum);
		userSecretly.setUser(peepUser);
		String questionUuid = secretly.getQuestionUuid();
		//偷偷看的问题
		Question question = questionService.getQuestionByUuid(questionUuid);
		userSecretly.setQuestion(question);
		
		model.addAttribute("userSecretly", userSecretly);
		return "/Secretly/view";
	}
	//红包的ttk方法，看的是问题，特别注意：该红包是偷看人收到的红包
	@RequestMapping("/secretly/viewDetailRed")
	public String viewDetailRed(Model model,
			@RequestParam(value = "questionUuid", required = true) String questionUuid) {
//		//偷偷看的对象
//		Secretly secretly = secretlyService.getSecretlyByUuid(questionUuid);
		UserSecretly userSecretly= new UserSecretly();
//		userSecretly.setSecretly(secretly);
//		String peepPhoneNum = secretly.getPeepPhoneNum();
//		//偷偷看的人，实际为收红包的人
//		User peepUser = userService.getUserByPhoneNumber(peepPhoneNum);
//		userSecretly.setUser(peepUser);
//		String questionId = secretly.getQuestionUuid();
		//偷偷看的问题
		Question question = questionService.getQuestionByUuid(questionUuid);
		userSecretly.setQuestion(question);
		//红包偷偷看对应的答案
		AnswerExample answerExample= new AnswerExample();
		com.jsb.model.AnswerExample.Criteria criteria = answerExample.createCriteria();
		criteria.andQuestionUuidEqualTo(questionUuid);
		criteria.andStatusEqualTo(2);//已采纳的答案
		List<Answer> answers = answerService.selectByExample(answerExample);
		if(answers!=null && answers.size()>0){
			userSecretly.setAnswer(answers.get(0));
		}
		model.addAttribute("userSecretly", userSecretly);
		return "/Secretly/viewDetailRed";
	}
	@RequestMapping("/secretly/moneyDetail")
	public String moneyDetail(Model model,
			@RequestParam(value = "secretlyUuid", required = true) String secretlyUuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		UserDealRecordExample dealRecordExample= new UserDealRecordExample();
		Criteria criteria = dealRecordExample.createCriteria();
		criteria.andBusinessNoEqualTo(secretlyUuid);
		int totalCount = userDealRecordService.countUserDealRecord(dealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		dealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		dealRecordExample.setPageSize(pageSize);
		
		dealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.selectByExample(dealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		return "/Secretly/moneyDetail";
	}
	@RequestMapping("/secretly/answerListTtk")
	public String answerListTtk(Model model,
		@RequestParam(value = "uuid", required = true) String uuid,
		@RequestParam(value = "pageNum", required = false) Integer pageNum,
		@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		AnswerExample example= new AnswerExample();
		com.jsb.model.AnswerExample.Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(2);
		criteria.andQuestionUuidEqualTo(uuid);
		int totalCount = answerService.countAnswer(example);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		example.setPageIndex((pageNum - 1) * pageSize);
		example.setPageSize(pageSize);
		
		example.setOrderByClause("date desc");
		List<Answer> answers = answerService.selectByExample(example);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		
		model.addAttribute("answers", answers);
		return "/Secretly/answerList";
	}
}